<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once 'Ressources/Constants.php';
require_once 'Classes/Model/Singleton.php';
require_once 'Classes/Exception/NoObjectInDatabaseException.php';

class PDOModel{
    
        

        private $conn;
        private $dsn = 'mysql:dbname=dsgf;host=127.0.0.1';
        private $user = 'dsgf';
        private $password = 'GYZXHLN8K2NqGLMw';
        
        protected $table = null;      // Ce tableau contient le nom des tables dans la base de données.
                                    // Pour éviter d'aller sur la BDD pour vérifier l'orthographe des tables..
        
        
        
        protected function __construct(){
            try{
                $this->conn = new PDO($this->dsn, $this->user, $this->password);
                
                $this->table = array();
                $this->table['article']= "Article";
                $this->table['clients']= "Clients";
                $this->table['families']= "Families";
                $this->table['loans']= "Loans";
                $this->table['structure']= "Structures";
                $this->table['subfamilies']= "Sub_Families";
                $this->table['users']= "Users";
                
                $this->conn->query("SET NAMES UTF8");
            }
            catch (PDOException $e){
                die('Unable to connect to database.');
            }
        }
        
         protected function PDORequest($sql,$data=array()){
        // Cette fonction est juste là pour gagner des lignes, et éviter de copier-coller la requete
        // PDO de partout.
        $req = $this->conn->prepare($sql);
        
         
        if($req->execute($data)){
           //var_dump($req);
            $dbData = $req->fetchAll(PDO::FETCH_ASSOC);
           
            if(!preg_match('/.*SELECT.*/i',$sql) || count($dbData) > 0){
                return $dbData;
            }
            else {
                throw new NoObjectInDatabaseException();
            }
        }
        else {
            die(var_dump($req->errorInfo()));
        }
    }
        
    }
    
?>
